package com.lincoln.framework.bean;

import com.lincoln.framework.ProtocolContextBean;
import com.lincoln.framework.interfaces.BaseDeviceInfo;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 服务器上下文
 *
 * @author lincoln
 * @date 2018/11/30
 */
public class ProtocolContext {
    public static ProtocolContextBean protocolContextBean = new ProtocolContextBean();

    /**
     * 初始化
     */
    public static void init() {
        protocolContextBean.init();
    }

    /**
     * 初始化
     */
    public static void init(Class baseDeviceInfoClass) {
        protocolContextBean.setBaseDeviceInfoClassType(baseDeviceInfoClass);
        protocolContextBean.init();
    }

    /**
     * 添加客户端
     *
     * @param deviceInfo
     * @return
     */
    public static boolean addClient(BaseDeviceInfo deviceInfo) {
        try {
            Map<String, BaseDeviceInfo> map = protocolContextBean.getClients().get(deviceInfo.getDeviceType());
            if (map == null) {
                protocolContextBean.getClients().put(deviceInfo.getDeviceType(), new ConcurrentHashMap<>());
                map = protocolContextBean.getClients().get(deviceInfo.getDeviceType());
            }
            map.put(deviceInfo.getClientId(), deviceInfo);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 通过内部主键clientId获取客户端
     *
     * @param deviceType
     * @param clientId
     * @return
     */
    public static <T extends BaseDeviceInfo> T getClient(int deviceType, String clientId) {
        // 检查是否在未登录里面
        if (protocolContextBean.getNoLoginClients().containsKey(clientId)) {
            return (T)protocolContextBean.getNoLoginClients().get(clientId);
        } else {
            try {
                return (T) protocolContextBean.getClients().get(deviceType).get(clientId);
            } catch (Exception e) {
                return null;
            }
        }

    }

    /**
     * 通过内部主键clientId踢出客户端
     *
     * @param clientId
     * @return
     */
    public static boolean removeClient(int deviceType, String clientId) {
        try {
            Map<String, BaseDeviceInfo> map = protocolContextBean.getClients().get(deviceType);
            if (map == null) {
                return true;
            }
            map.remove(clientId);
            return true;
        } catch (Exception e) {
            return false;
        }
    }


    /**
     * 向未登录列表中加入一个clientId
     *
     * @param deviceInfo
     * @return
     */
    public static boolean addNoLogin(BaseDeviceInfo deviceInfo) {
        if (protocolContextBean.getNoLoginClients().containsKey(deviceInfo.getClientId())) {
            return true;
        }
        protocolContextBean.getNoLoginClients().put(deviceInfo.getClientId(), deviceInfo);
        return true;
    }

    /**
     * 从未登录列表中删除一个clientId
     *
     * @param clientId
     * @return
     */
    public static boolean removeNoLogin(String clientId) {
        protocolContextBean.getNoLoginClients().remove(clientId);
        return true;
    }

    /**
     * 获取当前设备
     *
     * @return
     */
    public static <T extends BaseDeviceInfo> T getDevice() {
        return (T) protocolContextBean.getDevice();
    }

    /**
     * 设置当前设备
     *
     * @return
     */
    public static <T extends BaseDeviceInfo> void setDevice(T deviceInfo) {
        protocolContextBean.setDevice(deviceInfo);
    }
}
